package ngamejava2;

import java.applet.Applet;
import java.applet.AudioClip;
import java.awt.AlphaComposite;
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.DisplayMode;
import java.awt.Font;
import java.awt.FontFormatException;
import java.awt.Frame;
import java.awt.Graphics2D;
import java.awt.GraphicsConfiguration;
import java.awt.image.BufferStrategy;
import java.awt.image.BufferedImage;
import java.awt.image.VolatileImage;
import java.io.File;
import java.io.IOException;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.HashMap;
import javax.imageio.ImageIO;
import javax.swing.JFrame;

public class CanvasSystemFacade implements iGameSystemFacade
{
	public static final int PLAIN = 0;
	public static final int BOLD = 1;
	public static final int ITALIC = 2;
	public static final int BOLD_ITALIC = BOLD | ITALIC;
	protected BaseGame gameCanvas;
	private boolean fullScreen;
	private BufferStrategy bufferStrategy;
	private HashMap<String, Font> fonts;

	public CanvasSystemFacade(BaseGame aCanvas)
	{
		fonts = new HashMap<String, Font>();
		gameCanvas = aCanvas;
		fullScreen = false;
	}

	public void load()
	{
		gameCanvas.createBufferStrategy(2);
		bufferStrategy = gameCanvas.getBufferStrategy();
	}

	public void unload()
	{
		bufferStrategy.dispose();
	}

	public void update(long currentTick)
	{
	}

	public void render(long currentFrame)
	{
		bufferStrategy.show();
	}

	public int getWidth()
	{
		return gameCanvas.getWidth();
	}

	public int getHeight()
	{
		return gameCanvas.getHeight();
	}

	public Graphics2D getGraphics()
	{
		return (Graphics2D) bufferStrategy.getDrawGraphics();
	}

	public GraphicsConfiguration getGraphicsConfiguration()
	{
		return gameCanvas.getGraphicsConfiguration();
	}

	public void setFullScreen(boolean value)
	{
		if (fullScreen != value)
		{
			if (value)
			{
				Container o = gameCanvas.getParent();
				int c = 0;
				while (o != null && !(o instanceof Frame) && !(o instanceof Applet) && c < 10)
				{
					o = o.getParent();
				}
				if (o instanceof Frame)
				{
					if (gameCanvas.getGraphicsConfiguration().getDevice().isFullScreenSupported())
					{
						gameCanvas.getGraphicsConfiguration().getDevice().setFullScreenWindow((Frame) o);
						//gameCanvas.getGraphicsConfiguration().getDevice().setDisplayMode(new DisplayMode(gameCanvas.getWidth(), gameCanvas.getHeight(), 32, 60));
						fullScreen = true;
					}
				} else if (o instanceof Applet)
				{
					System.out.println("Applet in fullscreen.");
					JFrame fullWindowFrame = new JFrame("nonono");
					fullWindowFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
					fullWindowFrame.setIgnoreRepaint(true);
					fullWindowFrame.setLayout(new BorderLayout());
					fullWindowFrame.setVisible(true);
					fullWindowFrame.add(gameCanvas);
					fullWindowFrame.pack();
					gameCanvas.requestFocus();
					gameCanvas.getGraphicsConfiguration().getDevice().setFullScreenWindow(fullWindowFrame);
					fullScreen = true;
				}
			} else
			{
				fullScreen = false;
				gameCanvas.getGraphicsConfiguration().getDevice().setFullScreenWindow(null);
			}
		}
	}

	public boolean isFullScreen()
	{
		return fullScreen;
	}

	public VolatileImage newAcceleratedImage(int width, int height)
	{
		return gameCanvas.getGraphicsConfiguration().createCompatibleVolatileImage(width, height, BufferedImage.TRANSLUCENT);
	}

	public BufferedImage newImage(int width, int height)
	{
		return gameCanvas.getGraphicsConfiguration().createCompatibleImage(width, height, BufferedImage.TRANSLUCENT);
	}

	public BufferedImage getImage(String fileName) throws IOException
	{
		URL url = getClass().getResource("/" + fileName);
		if (url == null)
		{
			throw new RuntimeException("A imagem /" + fileName + " não foi encontrada.");
		} else
		{
			BufferedImage tmp = ImageIO.read(url);
			BufferedImage img = newImage(tmp.getWidth(), tmp.getHeight());
			Graphics2D g = img.createGraphics();
			g.setComposite(AlphaComposite.Src);
			g.drawImage(tmp, 0, 0, null);
			g.dispose();
			return img;
		}
	}

	public AudioClip getAudio(String fileName)
	{
		URL url = getClass().getResource("/" + fileName);
		if (url == null)
		{
			throw new RuntimeException("O áudio /" + fileName + " não foi encontrado.");
		} else
		{
			AudioClip clip = Applet.newAudioClip(getClass().getResource("/" + fileName));
			return clip;
		}
	}

	public Font getFont(String fileName, int size, int style)
	{
		URL url = getClass().getResource("/" + fileName);
		if (url == null)
		{
			throw new RuntimeException("A fonte /" + fileName + " não foi encontrada.");
		} else
		{
			try
			{
				Font font = fonts.get(fileName);
				if (font == null)
				{
					File file = new File(url.toURI());
					font = Font.createFont(Font.TRUETYPE_FONT, file);
					fonts.put(fileName, font);
				}
				font = font.deriveFont((float) size);
				if ((style & BOLD) == BOLD)
				{
					font = font.deriveFont(Font.BOLD);
				}
				if ((style & ITALIC) == ITALIC)
				{
					font = font.deriveFont(Font.ITALIC);
				}
				return font;
			} catch (FontFormatException ex)
			{
				throw new RuntimeException(ex);
			} catch (IOException ex)
			{
				throw new RuntimeException(ex);
			} catch (URISyntaxException ex)
			{
				throw new RuntimeException(ex);
			}
		}
	}
}
